/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.inquery;

import com.x3.purchasing.account.Account;
import com.x3.purchasing.common.ClassApplicationModule;
import com.x3.purchasing.operator.Operator;
import com.x3.purchasing.operator.OperatorDAO;
import com.x3.purchasing.operator.OperatorDAOImpl;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author kris
 */
public class InqueryWnd extends ClassApplicationModule {

    private Textbox txtJudul;
    private Textbox txtKeterangan;
    private Datebox dateOpen;
    private Datebox dateClose;
    private Listbox listboxData;
    private Inquery inqueryEdit;
    private InqueryDAO inqueryDAO;
    private OperatorDAO operatorDAO;
    private Session session;
    private Account account;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");

    public void onCreate() throws SQLException {
        txtJudul = (Textbox) getFellow("txtJudul");
        txtKeterangan = (Textbox) getFellow("txtKeterangan");
        dateOpen = (Datebox) getFellow("dateOpen");
        dateClose = (Datebox) getFellow("dateClose");
        listboxData = (Listbox) getFellow("listboxData");
        inqueryDAO = new InqueryDAOImpl();
        operatorDAO = new OperatorDAOImpl();
        session = Sessions.getCurrent();
        account = (Account) session.getAttribute("ACC_LOGIN");
        this.loadDataInquery();
    }

    public void loadDataInquery() {
        listboxData.getItems().clear();
        List<Inquery> inquerys;
        try {
            inquerys = inqueryDAO.gets(account.getId());
            int index = 1;
            for (final Inquery inquery : inquerys) {
                Listitem listitem = new Listitem();
                listitem.appendChild(new Listcell(index + ""));
                listitem.appendChild(new Listcell(inquery.getJudul()));
                listitem.appendChild(new Listcell(inquery.getKeterangan()));
                listitem.appendChild(new Listcell(dateFormat.format(inquery.getTanggalBuka())));
                listitem.appendChild(new Listcell(dateFormat.format(inquery.getTanggalTutup())));
                Toolbarbutton btnEdit = new Toolbarbutton();
                btnEdit.setId("btnEdit" + inquery.getId());
                btnEdit.setTooltiptext("Klik untuk mengubah data");
                btnEdit.setImage("/img/edit.png");
                btnEdit.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        inqueryEdit = inquery;
                        txtJudul.setValue(inquery.getJudul());
                        txtKeterangan.setValue(inquery.getKeterangan());
                        dateOpen.setValue(inquery.getTanggalBuka());
                        dateClose.setValue(inquery.getTanggalTutup());
                    }
                });

                Toolbarbutton btnDelete = new Toolbarbutton();
                btnDelete.setId("btnDelete" + inquery.getId());
                btnDelete.setTooltiptext("Klik untuk mengubah data");
                btnDelete.setImage("/img/delete.png");
                btnDelete.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        delete(inquery);
                    }
                });

                Toolbarbutton btnDetail = new Toolbarbutton();
                btnDetail.setId("btnDetail" + inquery.getId());
                btnDetail.setTooltiptext("Klik untuk melihat detail data");
                btnDetail.setImage("/img/detail.png");
                btnDetail.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        session.setAttribute("inqueryId", inquery.getId());
                        redirect("/zul/InqueryDetailOperator.zul");
                    }
                });
                Listcell cellAksi = new Listcell();
                cellAksi.appendChild(btnDetail);
                if (inquery.getTanggalTutup().getTime() > new Date().getTime()) {
                    cellAksi.appendChild(btnEdit);
                    cellAksi.appendChild(btnDelete);
                }
                listitem.appendChild(cellAksi);
                listboxData.appendChild(listitem);
                index++;
            }
        } catch (Exception ex) {
            Logger.getLogger(InqueryWnd.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void save() throws InterruptedException {
        Inquery inquery = new Inquery();
        inquery.setJudul(txtJudul.getText());
        inquery.setKeterangan(txtKeterangan.getText());
        inquery.setTanggalBuka(dateOpen.getValue());
        inquery.setTanggalTutup(dateClose.getValue());
        if (inquery.getJudul().isEmpty() || inquery.getKeterangan().isEmpty() || inquery.getTanggalBuka() == null || inquery.getTanggalTutup() == null) {
            Messagebox.show("Data ada yang kosong");
        } else {
            if (inqueryEdit == null) {
                try {
                    Operator operator = operatorDAO.getByAccount(account.getId());
                    inquery.setOperator(operator);
                    inqueryDAO.insert(inquery);
                    Messagebox.show("Data telah disimpan");
                    this.loadDataInquery();
                    this.clear();
                } catch (Exception ex) {
                    Logger.getLogger(InqueryWnd.class.getName()).log(Level.SEVERE, null, ex);
                    Messagebox.show("Gagal menyimpan data");
                }
            } else {
                try {
                    Operator operator = operatorDAO.getByAccount(account.getId());
                    inquery.setOperator(operator);
                    inquery.setId(inqueryEdit.getId());
                    inqueryDAO.update(inquery);
                    Messagebox.show("Data telah disimpan");
                    this.loadDataInquery();
                    this.clear();
                } catch (Exception ex) {
                    Logger.getLogger(InqueryWnd.class.getName()).log(Level.SEVERE, null, ex);
                    Messagebox.show("Gagal menyimpan data");
                }
            }
        }
    }

    public void cancel() {
        this.clear();
    }

    private void clear() {
        inqueryEdit = null;
        txtJudul.setText("");
        txtKeterangan.setText("");
        dateOpen.setValue(new Date());
        dateClose.setValue(new Date());
    }

    private void delete(Inquery inquery) throws InterruptedException {
        try {
            inqueryDAO.delete(inquery.getId());
            Messagebox.show("Data telah dihapus");
            this.loadDataInquery();
        } catch (Exception ex) {
            Logger.getLogger(InqueryWnd.class.getName()).log(Level.SEVERE, null, ex);
            Messagebox.show("Gagal menghapus data");
        }
    }

    public void redirect(String url) throws Exception {
        Div contentDiv = (Div) this.getParent();
        contentDiv.getChildren().clear();
        Executions.createComponents(url, contentDiv, null);
    }
}
